﻿$(document).ready(function () {
    getDepartments('DDL_Department','Select');
    getEmployeeDDL();
});
function saveEmployee() {
    var employee = new Object();
    employee.EmpName = $("[id$='txtEmpName']").val();
    employee.Address = $("[id$='txtAddress']").val();
    employee.Cell = $("[id$='txtCellNo']").val();
    employee.City_ID = $("[id$='hdnCityId']").val();
    employee.Dept_ID = $("[id$='DDL_Department']").val();
    employee.SubDeptID = $("[id$='DDL_SubDepartment']").val();
    $.ajax({
        url: "/HR/AddEditEmployee.aspx/SaveEmployee",
        type: "POST",
        dataType: "json",
        data: JSON.stringify({ employee: employee }),
        contentType: "application/json; charset=utf-8"
    }).done(function (data) {
        showSuccessMsg("Emplyee Added Successfully!!!!!");
        $("#divEmployeeWrapper").find("input[type=text]").val("");
        $("#divEmployeeWrapper").find("input[type=textarea]").val("");
        $("#divEmployeeWrapper").find("select").val("");
    });
}

function resetEmployee() {
    $("[id$='txtEmpName']").val("");
    $("[id$='txtAddress']").val("");
    $("[id$='txtCellNo']").val("");
    $("[id$='txtCity']").val("");
    $("[id$='hdnCityId']").val("");
}

function searchEmployee() {
    var employee = new Object();
    employee.Dept_ID = $("[id$='DDL_Department']").val();
    employee.SubDeptID = $("[id$='DDL_SubDepartment']").val();
    $.ajax({
        url: "/HR/SearchEmployee.aspx/SearchEmployee",
        type: "POST",
        dataType: "json",
        data: JSON.stringify({ Dept_Id: employee.Dept_ID,SubDept_Id:employee.SubDeptID }),
        contentType: "application/json; charset=utf-8"
    }).done(function (data) {
        var employees = data.d;
        for (var b = 0; b < employees.length; b++) {
            var html = "<tr>";
            html += "<td>" + employees[b].EmpId + "</td>";
            html += "<td>" + employees[b].EmpName + "</td>";
            html += "<td>" + employees[b].Address + "</td>";
            html += "<td>" + employees[b].Cell + "</td>";
            html += "<td>" + employees[b].BranchName + "</td>";
            html += "<td>" + employees[b].Dept_Name + "</td>";
            html += "<td>" + employees[b].SubDeptName + "</td>";
            html += "<td>" + employees[b].City_Name + "</td>";
            html += "</tr>";
            $("#tbodyEmployees").append(html);
        }
    });
}


function getExpirePolicy(ddlDepartment) {
    $.ajax({
        url: "../CommanMethods.aspx/GetDepartments",
        type: "POST",
        dataType: "json",
        data: {},
        contentType: "application/json; charset=utf-8"
    }).done(function (data) {
        var department = data.d;
        var html = "<ul>";
        if (firstText != null) {
            html += "<option value='0'>" + firstText + "</option>";
        }
        for (var d = 0; d < department.length; d++) {
            html += "<option value='" + department[d].Dept_ID + "'>" + department[d].Dept_Name + "</option>";
        }
        $("[id$='" + ddlDepartment + "']").html(html);
        if (needSubDept) {

            getSubDepartments(ddlSubDepartment, ddlDepartment);
        }
    });
}

function getEmployeeDDL() {
    $.ajax({
        url: "/HR/SearchEmployee.aspx/SearchEmployee",
        type: "POST",
        dataType: "json",
        data: JSON.stringify({ Dept_Id: 0, SubDept_Id: 0 }),
        contentType: "application/json; charset=utf-8"
    }).done(function (data) {
        var employees = data.d;
        var html = "";

        for (var d = 0; d < employees.length; d++) {
            html += "<option value='" + employees[d].EmpId + "'>" + employees[d].EmpName + "</option>";
        }
        $("[id$='" + ddl_employee + "']").html(html);
       
    });
}

function passwordChanged() {
    debugger;
    var pass = $("[id$='txtPassword']").val();
    var cPass = $("[id$='txtConfirmPassword']").val();
    if ((pass == null || pass == "") || (cPass == null || cPass == "")) {
        $('#message').html('').css('color', 'green');
    }
    else if (pass == cPass && pass != null && pass != "" && cPass != null && cPass != "") {
        $('#message').html('Matching').css('color', 'green');
    } else
        $('#message').html('Not Matching').css('color', 'red');
}

function SaveNewUser() {
    var user = new Object();
    user.EmpId = $("[id$='ddl_employee']").val();
    user.UserName = $("[id$='txtUserName']").val();
    user.Password = $("[id$='txtPassword']").val();
    $.ajax({
        url: "/HR/CreateNewUser.aspx/SaveNewUser",
        type: "POST",
        dataType: "json",
        data: JSON.stringify({ user: user }),
        contentType: "application/json; charset=utf-8"
    }).done(function (data) {
        showSuccessMsg("Login Created Successfully!!!!!");
        $("#divuserWrapper").find("input[type=text]").val("");
        $("#divuserWrapper").find("input[type=textarea]").val("");
        $("#divuserWrapper").find("select").val("");
    });
}